package com.breakfast.fun.ui.user.invite;

import com.breakfast.fun.data.entity.user.invite.Invite;
import com.breakfast.fun.data.entity.user.invite.InviteData;
import com.breakfast.fun.data.entity.user.Result;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/24.
 */
public class UserInvitePresenter extends UserInviteContract.Presenter {
    @Override
    public void getInvite() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        showLoadingDialog(true);
        rxManager.add(model.getInviteModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Invite>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Invite invite) {
                        hideLoadingDialog();
                        if (invite.isSuccess()) {
                            L.d("invite.getData().size()" + invite.getData().size());
                            view.showInvite(invite.getData());
                        } else {
                            T.show(invite.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void remind(InviteData data) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("phone", data.getPhone());

        showLoadingDialog(true);
        rxManager.add(model.getRemindModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Result result) {
                        hideLoadingDialog();
                        if (result.isSuccess()) {
                            getInvite();
                        } else {
                            T.show(result.getMsg());
                        }
                    }

                }));
    }


    @Override
    public void onStart() {
        getInvite();
    }
}
